package com.yyr.callmessage.core.qo;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

public class BaseQo {
    @Setter
    private String localUserJson;
    @Setter
    private String headerJson;
    private Object localUser;
    private HttpHeaderQo header;

    public <T> T getLocalUser(Class<T> classOfT) {
        if (localUser != null) {
            return (T)localUser;
        }

        if (StringUtils.isBlank(localUserJson)) {
            return null;
        }

        Gson gson = new GsonBuilder().create();
        localUser = gson.fromJson(localUserJson, classOfT);

        return (T)localUser;
    }

    public HttpHeaderQo getHeader() {
        if (header != null) {
            return header;
        }

        if (StringUtils.isBlank(headerJson)) {
            return null;
        }

        Gson gson = new GsonBuilder().create();
        header = gson.fromJson(headerJson, HttpHeaderQo.class);
        return header;
    }
}
